﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace rbdesign.Model
{
    class ModelArticle
    {
        public static SortedList<int, string> fGetArticlesList()
        {
            try
            {
                DataTable dt = ModelCRUD.Select("SELECT id,title,published FROM tblArticle");

                SortedList<int, string> listArticleTitles = new SortedList<int, string>();

                foreach (DataRow drRow in dt.Rows)
                {
                    string title = drRow["title"].ToString();
                    if (int.Parse(drRow["published"].ToString()) == 0)
                        title += " (בהמתנה)";
                    listArticleTitles.Add(int.Parse(drRow["id"].ToString()), title);
                }

                return listArticleTitles;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static List<int> fGetTopicsInArticlesList()
        {
            try
            {
                DataTable dt = ModelCRUD.Select("SELECT topic FROM tblArticle");

                List<int> listTopics = new List<int>();

                foreach (DataRow drRow in dt.Rows)
                {
                    listTopics.Add(int.Parse(drRow["topic"].ToString()));
                }

                return listTopics;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static bool fAddArticle(string _title, int _subject, int _topic)
        {
            try
            {
                ModelCRUD.InsertUpdateDelete("INSERT INTO tblArticle(title,subject,topic) Values('" + _title + "'," + _subject + "," + _topic + ")");

                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static bool fDeleteArticle(int _article)
        {
            try
            {
                ModelCRUD.InsertUpdateDelete("DELETE FROM tblArticle WHERE id='" + _article + "';");

                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
